package com.flaskpy.web.eternity.admin.controller;/*-----------------------------------
 *
 * flaskpy官方专业代码Copy大师 Administrator
 * 在 2021/6/8 21:54 创建这伟大又永久
 * 没有bug的文件
 *
 *-----------------------------------/


 /**
 * @Author flaskpy-Administrator
 * @Date 2021/6/8 21:54
 * @Version 1.0
 */


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.RandomUtil;
import com.flaskpy.web.eternity.admin.param.LoginParam;
import com.flaskpy.web.eternity.admin.vo.LoginVO;
import com.flaskpy.web.eternity.auth.jwt.context.JwtContextHolder;
import com.flaskpy.web.eternity.core.api.R;
import com.flaskpy.web.eternity.core.api.ResponseData;
import com.flaskpy.web.eternity.core.api.SuccessResponse;
import com.flaskpy.web.eternity.core.cache.abs.ICacheOperation;
import com.flaskpy.web.eternity.core.captcha.EyCaptcha;
import com.flaskpy.web.eternity.core.exception.BizException;
import com.flaskpy.web.eternity.core.holder.RequestHolder;
import com.flaskpy.web.eternity.core.holder.RequestNoHolder;
import com.flaskpy.web.eternity.log.aspect.AopLog;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/auth")
public class AuthController {
    @Resource(name = "captchaCache")
    private final ICacheOperation<String> captchaCache;
    @PostMapping("/login")
    public ResponseData<LoginVO> login(@RequestBody @Validated LoginParam loginParam) {
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
                new UsernamePasswordAuthenticationToken(loginParam.getUserName(), loginParam.getPassword());
        Authentication authentication = JwtContextHolder.login(usernamePasswordAuthenticationToken);
        LoginVO loginVO = new LoginVO();
        loginVO.setToken(JwtContextHolder.currentJwtToken());
        loginVO.setJwtPayLoad(JwtContextHolder.currentJwtPayload());
        return R.success(loginVO);
    }

    @PostMapping("/logout")
    public ResponseData logout() {
        if (JwtContextHolder.currentJwtToken() == null)
            throw new BizException();
        JwtContextHolder.logout();
        return R.success();
    }
    @SneakyThrows
    @GetMapping("/code1")
    public void code1(HttpServletRequest request, HttpServletResponse response){
        EyCaptcha captcha=new EyCaptcha(100,40,4,3);
        ServletOutputStream out = response.getOutputStream();
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        captcha.write(out);
    }
    @GetMapping("/code2")
    public SuccessResponse code2(HttpServletRequest request){
        EyCaptcha captcha=new EyCaptcha(100,40,4,3);
        String codeValue=captcha.getCode();
        String uuid= RequestNoHolder.get().split("-")[4];   //省空间
        Map<String,String> data= new HashMap<>();
        data.put("uuid",uuid);
        data.put("base64",captcha.getImageBase64Data());
        captchaCache.set(uuid,codeValue,180);
        return R.success(data);
    }

    @PreAuthorize("hasRole('admin')")
    @GetMapping("/test1")
    public ResponseData test1() {

        return R.success(RandomUtil.randomString(10));
    }

    @PreAuthorize("hasAuthority('add')")
    @GetMapping("/test2")
    public ResponseData test2() {

        return R.success(RandomUtil.randomString(10));
    }

    @PreAuthorize("hasAuthority('remove')")
    @GetMapping("/test3")
    public ResponseData test3() {

        return R.success(RandomUtil.randomString(10));
    }

    @PreAuthorize("hasAuthority('remove')")
    @GetMapping("/test4")
    public ResponseData test4() {

        return R.success(RandomUtil.randomString(10));
    }
}
